<template>
    <div>
        <rui-page :defines="defines" v-show="approveShow">
            <rui-query :label-width="150"
                       ref="searchForm"
                       :query="queryData"
                       :query-rows="searchFormOptions"
            />
            <rui-table
                :defineId="'definePlan'"
                :fixedLeft="['action']"
                :showIndex="false"
                :select="false"
                :slots="[{key:'action',slot:'action'}]"
                :columns="planColumns"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="planTable"
            >
                <template slot="toolBar">
                    <Button @click="add" type="primary" icon="md-add">新增</Button>
                </template>
                <template slot="action" slot-scope="{ row }">
                    <Button @click="view(row)" size="small" type="primary" v-show="row.status!=0">查看</Button>
                    <Button @click="edit(row)" size="small" type="primary" v-show="row.status==0">编辑</Button>
                    <Button type="error" size="small" @click="remove(row)"  v-show="row.status=='0'" >删除</Button>
                </template>
            </rui-table>

            <Modal
                v-model="templateShow"
                :title="modalTitle"
                :closable="false"
                :width="1000"
                :mask-closable="false"
            >
                <rui-query :label-width="180" ref="searchTemplateForm"
                           :query="queryTemplateData" :query-rows="searchTemplateFormOptions"/>
                <rui-table
                    :defineId="'defineParentTemplate'"
                    :fixedLeft="['action']"
                    :showIndex="false"
                    :columns="parentTemplateColumns"
                    @loadDatas="queryTemplateData"
                    @on-selection-change="changeTemplateSelect"
                    ref-query="searchTemplateForm"
                    ref="parentTemplateTable"
                >
                </rui-table>
                <Row class="operation" style="text-align: center;">
                    <Button @click="next" type="primary" icon="md-fastforward">下一步</Button>
                    &nbsp;&nbsp;
                    <Button @click="cancel" type="primary" icon="md-rewind">取消</Button>
                </Row>
                <div slot="footer">
                </div>
            </Modal>

        </rui-page>

        <productUpdateTab :planId="planId" :op="op" :parentParam="parentParam" @backPage="backPage" v-if="!approveShow" @saveResult="saveResult" />

    </div>
</template>
<script>

    //产品修改方案展示字段
    import planDefine from "./define/plan-define";
    //产品修改方案查询
    import {
        getApprovePlanList,
        delUpdateById,
    } from "@/projects/afs-core-business/api/afs-product/product-plan-approve/approve";

    //模板展示字段
    import defineParentTemplate from "../defines/parent-template-define";
    //模板查询方法
    import {getListProductTemplateEffective} from "@/projects/afs-core-business/api/afs-product/product/product";

    //修改页面组件导入
    import productUpdateTab from "./productUpdateTab.vue";

    export default {
        components: {
            productUpdateTab
        },
        data() {
            return {
                //展示判断
                approveShow: true,
                //列表查询字段
                searchFormOptions: [
                    {defineId: "definePlan", span: 6, fields: ["planNumber","planName","effectiveDate","status"]},
                ],
                //列表展示字段
                planColumns: ['action',"planNumber","planName", 'effectiveDate', 'status'],

                //模板是否展示
                templateShow: false,
                modalTitle: '选择修改模板',
                //模板查询
                searchTemplateFormOptions: [
                    {defineId: "defineParentTemplate", fields: ["templateNumber", "templateName"]}
                ],
                //模板展示
                parentTemplateColumns: ['templateNumber', 'templateName', 'templateCategory', 'versionNumber'],

                //组件调用
                planId:null,
                op:'view',
                parentParam:{},

                operationLoading: false,
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "definePlan",
                        fields: planDefine
                    },
                    {
                        id: 'defineParentTemplate',
                        fields: defineParentTemplate
                    }
                ];
            }
        },
        methods: {
            //刷新
            refresh() {
                this.$refs['planTable'].reloadData();
            },
            //列表查询
            queryData(queryData){
                // console.info(JSON.stringify(queryData));
                getApprovePlanList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.planTable.updateTableData(records, total);
                    }
                });
            },
            //查看详情
            view(v) {
                this.approveShow=false;
                this.planId=v.id;
                this.op='view';
                this.parentParam = {
                    status: 'view',
                    productTemplateId: v.productTemplateId,
                    id: v.id,
                    templateCategory: v.templateCategory
                };
            },
            //编辑
            edit(v) {
                this.approveShow=false;
                this.planId=v.id;
                this.op='edit';
                this.parentParam = {
                    status: 'edit',
                    productTemplateId: v.productTemplateId,
                    id: v.id,
                    templateCategory: v.templateCategory
                };
            },
            //删除
            remove(v){
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除产品修改信息: " + v.planName + " ?",
                    onOk: () => {
                        this.operationLoading = true;
                        delUpdateById(v.id).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            //新增
            add() {
                this.selectTemplateCount = 0;
                this.templateShow = true;
                this.approveShow=true;
                this.$refs['parentTemplateTable'].reloadData();
                this.$refs.parentTemplateTable.getTable().selectAll(false);
            },
            //查询修改模板
            queryTemplateData(queryData) {
                getListProductTemplateEffective(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.parentTemplateTable.updateTableData(records, total);
                    }
                });
            },
            //模板选择
            changeTemplateSelect(e) {
                this.selectTemplateList = e;
                this.selectTemplateCount = e.length;
            },
            //下一步
            next() {
                if (this.selectTemplateCount <= 0) {
                    this.$Message.warning("您还未选择数据！");
                    return;
                } else if (this.selectTemplateCount > 1) {
                    this.$Message.warning("您只能选择一笔数据！");
                    return;
                }
                let id = null;
                let templateCategory = null;
                this.selectTemplateList.forEach((e) => {
                    id = e.id;
                    templateCategory = e.templateCategory;
                });
                this.templateShow = false;
                this.approveShow = false;

                this.parentParam = {status: 'add', productTemplateId: id, templateCategory: templateCategory};
                this.planId=null;
                this.op='add';
                console.info("修改方案跳转的值："+JSON.stringify(this.parentParam));
            },
            //取消
            cancel() {
                this.templateShow = false;
                this.approveShow = true;
            },
            //组件返回方法
            backPage() {
                this.templateShow = false;
                this.approveShow = true;
                this.refresh();
            },
            //组件保存返回
            saveResult(id){
                this.planId = id;
                this.op = "edit";
            },
        },
        mounted() {
            this.refresh();
        },
    };
</script>
